package com.lhf.common.util;

import com.lhf.common.enums.Status;

import java.io.Serializable;

/**
 * <p></p>
 *
 * @author liuhf
 * @createTime 2021/3/29 8:55
 * @since 1.0
 */
public class Result<T> implements Serializable {


    private static final long serialVersionUID = -7917578586590084270L;

    private int code;
    private T data;
    private String mes;

    public Result() {
    }

    public Result(int code, T data, String mes) {
        this.code = code;
        this.data = data;
        this.mes = mes;
    }

    public Result(int code, String mes) {
        this.code = code;
        this.mes = mes;
    }

    /**
     * 请求成功
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T t) {
        Status success = Status.SUCCESS;
        return new Result<>(success.code, t, success.mes);
    }

    /**
     * 请求成功
     * 自定义状态
     *
     * @param status
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(Status status, T t) {
        return new Result<>(status.code, t, status.mes);
    }

    /**
     * 请求成功
     * 自定义状态
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(int code, T t, String mes) {
        return new Result<>(code, t, mes);
    }

    /**
     * 请求失败
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure() {
        return new Result<>(Status.FAILURE.code, Status.FAILURE.mes);
    }

    /**
     * 请求失败，自定义状态
     *
     * @param status
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(Status status) {
        return new Result<>(status.code, status.mes);
    }

    /**
     * 请求失败，自定义状态
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(int code, String mes) {
        return new Result<>(code, mes);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMes() {
        return mes;
    }

    public void setMes(String mes) {
        this.mes = mes;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", data=" + data +
                ", mes='" + mes + '\'' +
                '}';
    }
}
